
import CommonAction from './common';


export default class OrgSituation extends CommonAction {
    constructor(comp) {
        super();
        this.comp = comp;
    }

    // 打开编制情况弹窗
    openOrgSituationModal = async (selectedRows) => {
        const {props, action, state} = this.comp;
        const {dispatch, emp, editTable} = props;

        this.comp.setState({
            selectedRows: this.deepCopy(selectedRows)
        });

        selectedRows = action.rowAct.getSelectedRows(selectedRows);

        if(selectedRows) {
            let postData = {
                billids: [],
                pk_org: emp.orgValue.refpk
            };

            selectedRows.map((item) => {
                postData.billids.push(item.values['pk_entryapply'].value);
            });

            let res = await dispatch({
                type: 'emp/getOrgSituationInitData',
                payload: {
                    postData: postData
                }
            });

            if(res.success) {
                const {opts, org, dept} = res.data;

                dispatch({
                    type: 'emp/update',
                    payload: {
                        orgSituationModalVisible: true,
                        orgStaOptions: opts,
                        orgStaOrgData: org.org,
                        orgStaDeptData: dept.dept,
                        orgStaOrgDimension: opts[0].value
                    }
                });
            }
        }
    }

    // 关闭弹窗
    closeModal = () => {
        this.comp.props.dispatch({
            type: 'emp/update',
            payload: {
                orgSituationModalVisible: false,
                orgStaOrgDimension: 'org'
            }
        });
        this.comp.setState({
            selectedRows: null
        });
    }

    // 切换编制维度
    changeOrgDimension = async (value) => {
        const {props, action, state} = this.comp;
        const {dispatch, emp} = props;

        let selectedRows = action.rowAct.getSelectedRows(state.selectedRows);

        if(selectedRows) {
            let postData = {
                billids: [],
                pk_org: emp.orgValue.refpk,
                pk_dimension: value
            };

            selectedRows.map((item) => {
                postData.billids.push(item.values['pk_entryapply'].value);
            });

            let res = await dispatch({
                type: 'emp/getOrgSituationInitData',
                payload: {
                    postData: postData
                }
            });

            if(res.success) {
                const {org, dept} = res.data;

                dispatch({
                    type: 'emp/update',
                    payload: {
                        orgStaOrgData: org.org,
                        orgStaDeptData: dept.dept,
                        orgStaOrgDimension: value
                    }
                });
                this.setOrgDeptTableData(org.org, dept.dept);
            }
        }
    }

    // 切换tab的时候
    changeTab = (value) => {
        this.comp.props.dispatch({
            type: 'emp/update',
            payload: {
                orgStaActiveTab: value
            }
        });
    }

    // 显示完了弹窗
    setOrgDeptTableData = (orgData, deptData) => {
        const {props} = this.comp;
        const {emp, editTable} = props;

        !orgData && (orgData = emp['orgStaOrgData']);
        !deptData && (deptData = emp['orgStaDeptData']);

        editTable.setTableData('org', orgData, false);
        editTable.setTableData('dept', deptData, false);
    }
}